package com.maxd.controller.weixin;
import com.maxd.adapter.IgnoreSecurity;
import com.maxd.service.weixin.WxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

/**
 * @author fang
 * @date 2020/2/26
 */
@Api(value = "微信支付", tags = {"微信支付"})
@RestController
@RequestMapping("/api/order")
public class ApiWeiXinPayController {

    private final Logger log = LoggerFactory.getLogger(ApiWeiXinPayController.class);

    @Autowired
    private WxService wxService;

    @ApiOperation("app支付")
    @PostMapping("/wxPayApp")
    public Map wxPayApp(Long userId,String money) throws Exception {
        return wxService.doUnifiedOrder(money,userId,1);
    }

    @ApiOperation("二维码支付")
    @PostMapping("/wxPayWeb")
    public Map wxPayWeb(Long userId,String money) throws Exception {
        return wxService.doUnifiedOrder(money,userId,2 );
    }

    @ApiOperation("公众号支付")
    @PostMapping("/wxPayJsApi")
    public Map wxPayJsApi(Long userId,String money) throws Exception {
        return wxService.doUnifiedOrder(money,userId,3);
    }


    @PostMapping("/notify")
    @ApiOperation("微信回调")
    @IgnoreSecurity
    public String wxPayNotify(HttpServletRequest request) {
        String resXml = "";
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                log.info(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = wxService.payBack(resXml,1);
            log.info("成功");
            log.info(result);
            return result;
        } catch (Exception e) {
            log.info("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            log.info("失败");
            log.info(result);
            return result;
        }
    }

    @PostMapping("/notifyJsApi")
    @ApiOperation("微信回调")
    @IgnoreSecurity
    public String notifyJsApi(HttpServletRequest request) {
        String resXml = "";
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                log.info(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = wxService.payBack(resXml,3);
            log.info("成功");
            log.info(result);
            return result;
        } catch (Exception e) {
            log.info("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            log.info("失败");
            log.info(result);
            return result;
        }
    }

}
